local Array =import("app.engine.struct.Array")
RushState =class("RushState",function () return StateBase.new()  end)
local rushSize =5
function RushState:onTiming()
 local interval =Context.interval
 local scene =display.getRunningScene()
  if viewController:isGameOver() then return end
  viewController:tick(interval)
  scene.updateTime =scene.updateTime+interval
  scene._time=scene._time+interval
  if scene._time>1 then
   scene.uiLayer:onUpdate()
   scene._time =0
  end
end

function RushState:onEnter()
   print("Rush Enter")
   local scene =display.getRunningScene()
   local layer =scene.AnimationLayer
   transition.stopTarget(layer)
   self:fishRush(scene.uiLayer.fish)
   local px,py =layer:getPosition()
   local fx,fy =scene.uiLayer.fish:getPosition()
   local point =CCNode.convertToWorldSpace(scene.uiLayer.fish,CCPoint(fx,fy))
   local s =layer.layouts:resizeTo(point.x,layer,rushSize)
   transition.moveBy(layer, {x =-s, time = 1,onComplete=handler(self,self.onExit)})
end

 function RushState:fishRush(fish)
 
   local chongci = display.newSprite("#chongci01.png",0,0)
   local frames =display.newFrames("chongci%02d.png",1,4)
   local animation =display.newAnimation(frames, 1.0 /20)
   fish:addChild(chongci)
   local x,y =fish.fish:getPosition()
   display.align(chongci,display.CENTER,x,display.cy);
   transition.playAnimationForever(chongci,animation)
   print("Fish rush")
   fish.chongci =chongci
   fish.chongci:retain()
   fish.fish:setVisible(false)

 end


function RushState:onPillarEnter(uiObject)
     StateBase.playXiaoShiUp(self,uiObject)
     StateBase.playXiaoShiDown(self,uiObject)

end

function RushState:onExit()
   print("rushEnd")
   
   local scene =display.getRunningScene()
   scene:move()
   scene.uiLayer.lastTime =nil
   scene.uiLayer:addScore(rushSize)
   self:fishRushEnd(scene.uiLayer.fish)
   self:animationLayerRushEnd(scene.AnimationLayer)
   viewController:setStateRunning()
end

function  RushState:animationLayerRushEnd(layer)
     local px,py =layer:getPosition()
     layer.layouts:resumeCreateUI(layer,px)
     
end
function RushState:fishRushEnd(fish)
     local fadeOut =CCFadeOut:create(1)
     transition.fadeOut(fish.chongci,{time=1,onComplete=handler(fish,fish.removeEffect)})   
  
end
